package com.huang.controller;

import com.huang.pojo.User;
import org.springframework.beans.propertyeditors.CurrencyEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.ContextExposingHttpServletRequest;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.HttpServletBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;


@Controller
@SessionAttributes("user")
public class HelloServlet {
    @InitBinder
    public void dataBinder(WebDataBinder binder){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyy-MM-dd");
        binder.registerCustomEditor(Data.class, new CustomDateEditor(dateFormat,true));
    }





    @RequestMapping("/hello")
    public String hello() {
        return "index.jsp";
    }
    @RequestMapping("/param1")
    @ResponseBody
    public void param1(String param1,String param2){
        System.out.println(param1);
        System.out.println(param2);
    }

    @RequestMapping("/param2")
    @ResponseBody
    public void param2(User user){
        System.out.println(user);
    }
//从路径中获取参数
    @RequestMapping("/param3/{username/{address}")
    @ResponseBody
    public void param3(@PathVariable String username,@PathVariable String address){
        System.out.println(username);
        System.out.println(address);
    }

    @RequestMapping("/param5")
    @ResponseBody
    public void param5(Model model){
        System.out.println(model.getAttribute("username"));
    }

//
//    @RequestMapping("/param6")
//    @ResponseBody
//    public void param6(Model username) {
//        System.out.println(@ModelAttribute String username);
//    }
    @GetMapping("/param7")
    public void handle(
            @RequestHeader("Accept-Encoding") String encoding,
            @RequestHeader("Keep-Alive") long keepAlive ){
        System.out.println(encoding);
        System.out.println(keepAlive);
    }
    @RequestMapping("/redirect")
    public String redirect(Model model) {
        model.addAttribute("usename","zhangsan");
        return "redirect:/redirect.jsp";
    }


    @RequestMapping("/forward")
    public String forward(Model model) {
        model.addAttribute("username","zhangsan");
                return "forward:/forward.jsp";
    }

    @RequestMapping("/demo1")
    @ResponseBody
    public void demo1(Model model){
        User user=new User();
        user.setName("zhangsan");
        user.setAddress("huangshi");
        model.addAttribute("user",user);
    }
    @RequestMapping("/demo2")
    @ResponseBody
    public void demo2(Model model){
        User user=(User) model.getAttribute("user");
        System.out.println(user);

    }
    @RequestMapping("/demo3")
    @ResponseBody
    public void demo3(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session= req.getSession();
        session.setAttribute("key","value");
    }

}
